package com.whm.hrmanagement.service.impl;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whm.common.core.domain.AjaxResult;
import com.whm.common.core.domain.model.LoginUser;
import com.whm.common.enums.ExamineStatusEnum;
import cn.hutool.core.date.DateUtil;

import com.whm.common.enums.OaStaffAttendanceEnum;
import com.whm.common.utils.DateUtils;
import com.whm.hrmanagement.domain.OaStaffAttendance;

import com.whm.hrmanagement.domain.dto.OaStaffQjDto;
import com.whm.hrmanagement.domain.dto.OaStaffQjInfoDto;
import com.whm.hrmanagement.domain.dto.OaStaffSalaryDto;
import com.whm.hrmanagement.domain.vo.AttendanceMonthVO;
import com.whm.hrmanagement.domain.vo.OaStaffQjVo;
import com.whm.hrmanagement.service.IOaStaffAttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.whm.hrmanagement.mapper.OaStaffQjMapper;
import com.whm.hrmanagement.domain.OaStaffQj;
import com.whm.hrmanagement.service.IOaStaffQjService;

import static com.whm.common.utils.PageUtils.startPage;

/**
 * 员工请假Service业务层处理
 * 
 * @author whm
 * @date 2024-02-05
 */
@Service
public class OaStaffQjServiceImpl  extends ServiceImpl<OaStaffQjMapper, OaStaffQj>  implements IOaStaffQjService
{
    @Autowired
    private OaStaffQjMapper oaStaffQjMapper;


    @Autowired
    private IOaStaffAttendanceService iOaStaffAttendanceService;

    /**
     * 查询员工请假
     * 
     * @param id 员工请假主键
     * @return 员工请假
     */
    @Override
    public OaStaffQj selectOaStaffQjById(Long id)
    {
        return oaStaffQjMapper.selectOaStaffQjById(id);
    }



    /**
     * 查询员工请假列表
     * 
     * @param oaStaffQj 员工请假
     * @return 员工请假
     */
    @Override
    public List<OaStaffQj> selectOaStaffQjList(OaStaffQj oaStaffQj)
    {
        return oaStaffQjMapper.selectOaStaffQjList(oaStaffQj);
    }

    @Override
    public   List<OaStaffQjDto>  selectOaStaffQjLis2(OaStaffQjVo oaStaffQjVo) {

String name = oaStaffQjVo.getName();
        if (name == null) {
            name = "";
        }
Long deptId = oaStaffQjVo.getDeptId();

        List<OaStaffQjInfoDto> oaStaffQjInfoDtoList = new ArrayList<>();
        if(deptId == null) {
            oaStaffQjInfoDtoList = oaStaffQjMapper.listStaffLeaveVO(ExamineStatusEnum.CANCEL.getCode(), name);
        }else{
            oaStaffQjInfoDtoList =oaStaffQjMapper.listStaffDeptLeaveVO(ExamineStatusEnum.CANCEL.getCode(),name,deptId);
        }

            LoginUser user = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user.getDeptId() != 204 && user.getDeptId() != 200 &&  user.getUserId() != 109 && user.getUserId() != 1) {

            startPage();
            oaStaffQjInfoDtoList =  oaStaffQjInfoDtoList.stream().filter(e -> e.getCode() == user.getUserId()).collect(Collectors.toList());
        }else {
            startPage();

            oaStaffQjInfoDtoList = oaStaffQjInfoDtoList;
        }


        List<OaStaffQjDto> list = new ArrayList<>();
        for (OaStaffQjInfoDto oaStaffQj1 : oaStaffQjInfoDtoList) {
            OaStaffQjDto oaStaffQjDto = new OaStaffQjDto();

            oaStaffQjDto.setOaStaffQjInfoDto(oaStaffQj1);
            oaStaffQjDto.setApprove(ExamineStatusEnum.APPROVE);
            oaStaffQjDto.setReject( ExamineStatusEnum.REJECT);
            oaStaffQjDto.setTagType(oaStaffQj1.getStatus().getTagType());
            oaStaffQjDto.setUnaudited(ExamineStatusEnum.UNAUDITED);

            list.add(oaStaffQjDto);
        }


        return list;
    }

    /**
     * 新增员工请假
     * 
     * @param oaStaffQj 员工请假
     * @return 结果
     */
    @Override
    public int insertOaStaffQj(OaStaffQj oaStaffQj)
    {
        oaStaffQj.setCreateTime(DateUtils.getNowDate());
        return oaStaffQjMapper.insertOaStaffQj(oaStaffQj);
    }

    /**
     * 修改员工请假
     * 
     * @param oaStaffQj 员工请假
     * @return 结果
     */
    @Override
    public AjaxResult updateOaStaffQj(OaStaffQj oaStaffQj)
    {
        if (oaStaffQj.getStatus() == ExamineStatusEnum.APPROVE) {
            for (int i = 0; i < oaStaffQj.getDays(); i++) {
                Date attendanceDate = DateUtil.offsetDay(oaStaffQj.getStartDate(), i).toSqlDate();
                // 因为周末本就要休息，所以只需记录在休假期间包括的工作日的考勤状态到数据库
                if (!DateUtil.isWeekend(attendanceDate)) {
                    OaStaffAttendance oaStaffAttendance = new OaStaffAttendance();
                    oaStaffAttendance.setAttendanceDate(attendanceDate);
                    oaStaffAttendance.setStaffId(oaStaffQj.getStaffId());
                    oaStaffAttendance.setStatus(OaStaffAttendanceEnum.LEAVE);
                    QueryWrapper<OaStaffAttendance> queryWrapper2 = new QueryWrapper<>();
                    queryWrapper2.eq("staff_id", oaStaffAttendance.getStaffId()).eq("attendance_date", oaStaffAttendance.getAttendanceDate());
                    if (!iOaStaffAttendanceService.saveOrUpdate(oaStaffAttendance,queryWrapper2)){
                        return AjaxResult.error();
                    }
                }
            }
        }
        if (updateById(oaStaffQj)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 批量删除员工请假
     * 
     * @param ids 需要删除的员工请假主键
     * @return 结果
     */
    @Override
    public int deleteOaStaffQjByIds(Long[] ids)
    {
        return oaStaffQjMapper.deleteOaStaffQjByIds(ids);
    }

    /**
     * 删除员工请假信息
     * 
     * @param id 员工请假主键
     * @return 结果
     */
    @Override
    public int deleteOaStaffQjById(Long id)
    {
        return oaStaffQjMapper.deleteOaStaffQjById(id);
    }
}
